/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */


#include <stdlib.h>
#include <string.h>
#include <aos/aos.h>
#include "aos/cli.h"
#include "app_main.h"
#include <aos/yloop.h>
#include <drv/codec.h>

#define TAG "app"

aos_timer_t tmr_ai;
static int asr_in_detection = 0;

static void cli_reg_cmds(void)
{
    extern void cli_reg_cmd_app(void);
    cli_reg_cmd_app();

    cli_reg_cmd_iperf();
    cli_reg_cmd_w800();
    cli_reg_cmd_ifconfig();
    cli_reg_cmd_sysinfo();
}

static void app_ai_cb(mvoice_event_e evt_id, void *data, int size)
{
    int ret;
    LOGD(TAG, "enter ai callback");
    // mvoice_process_pause();
    
    switch(evt_id) {
        case MVC_EVT_VAD_START:
            break;
        case MVC_EVT_VAD_END:
            break;
        case MVC_EVT_KWS:
            break;
        case MVC_EVT_ASR:
            LOGD(TAG, "kws detect %s in_detection=%d", (char *)data, asr_in_detection);
            int asr_spot = 0;

#if defined(CONFIG_IR_DEMO) && CONFIG_IR_DEMO
            if (strcmp(data, "小智同学") == 0) {
#else
            if (strcmp(data, "小蜂管家") == 0) {
#endif
                /* main kws restart the 10 s timer */
                asr_spot = 1;
                local_audio_play("我在", 0);

            } else {
                /* asr command only valid in the 10 s windows after kws */
                if (asr_in_detection) {
#if !defined(CONFIG_IR_DEMO) || !CONFIG_IR_DEMO
                    /* play the asr string if local audio available */
                    ret = local_audio_play((const char *)data, 0);
                    if (ret == 0) {
                        asr_spot = 1;
                    }
#endif

                    ret = iotdispatcher_voice_set((char *)data);
#if defined(CONFIG_IR_DEMO) && CONFIG_IR_DEMO
                    if (ret != 0) {
                        local_audio_play("不支持该功能", 0);
                    }
                    asr_spot = 1;
#endif
                }
            }
            
            if (asr_spot) {
                LOGD(TAG, "restart asr detection");
                aos_timer_stop(&tmr_ai);
                aos_timer_start(&tmr_ai);
                asr_in_detection = 1;
            }
                
            break;
        default:
            break;
    }

    // mvoice_process_resume();
    LOGD(TAG, "leave ai callback");
}

static void ai_timer_cb(void *timer, void *arg)
{
    LOGD(TAG, "asr detection timeout");

    asr_in_detection = 0;
#if defined(CONFIG_IR_DEMO) && CONFIG_IR_DEMO
    local_audio_play("没事我先休息了", 1);
#else
    local_audio_play("退下了", 1);
#endif
}

static int app_ai_init()
{
#if defined(CONFIG_IR_DEMO) && CONFIG_IR_DEMO
    aos_timer_new_ext(&tmr_ai, ai_timer_cb, NULL, 30000, 0, 0);
#else
    aos_timer_new_ext(&tmr_ai, ai_timer_cb, NULL, 10000, 0, 0);
#endif

    mvoice_register_huazhen(app_ai_cb);
    mvoice_process_start();
}

static int app_ir_init()
{
    // app_spi_flash_init();
    remote_data_init();
}

int main()
{
    int tick_counter = 0;
    board_yoc_init();
    LOGD(TAG, "%s\n", aos_get_app_version());
    // aos_set_log_level(AOS_LL_INFO);

    event_service_init(NULL);

    app_sys_init();

    board_base_init();

    sys_event_init();

//    bt_w800_register();

#if defined(CONFIG_IR_DEMO) && CONFIG_IR_DEMO
    app_ir_init();
#endif

    cli_reg_cmds();

    int ret = csi_codec_init(0);
    if (ret < 0) {
        LOGE(TAG, "codec init failed");
    }

    local_audio_init();

    app_ai_init();

    app_iot_init();

#if defined(APP_FOTA_EN) && APP_FOTA_EN
    app_fota_init();
#endif

#if defined(CONFIG_IR_DEMO) && CONFIG_IR_DEMO
    LOGD(TAG, "IR demo start");
    local_audio_play("欢迎使用语音智能伴侣", 0);
    return 0;
#else
    LOGD(TAG, "LED demo start");
    wifi_mode_e mode = app_network_init();


    if (app_sys_get_boot_reason() == BOOT_REASON_WIFI_CONFIG || app_sys_get_boot_reason() == BOOT_REASON_SILENT_RESET) {
        LOGD(TAG, "start reason %d", app_sys_get_boot_reason());
    } else {
        local_audio_play("欢迎使用小蜂管家智能开关面板", 0);
    }
        
    app_pwm_led_init();
#endif

    return 0;
}
